<template>
	<view class="main_bg">
		<app-header :title="typeName"></app-header>
		<view class="search flex-c">
			<view class="input flex-s">
				<u-icon name="search" color="#8C8A8A" size="48rpx"></u-icon>
				<!-- <input v-model="query.name" placeholder="请输入设备名称" /> -->
				<u--input placeholder="请输入设备名称" border="none" v-model="query.name" clearable></u--input>
				<view class="btn flex-c" @click="search">搜索</view>
			</view>
		</view>
		<scroll-view scroll-y="true" :refresher-triggered="triggered" class="scroll-y" @refresherrefresh="handleRefresh"
			@scrolltolower="lower" :style="{height:scrollHeight}" :enhanced="true" :show-scrollbar="true">
			<view class="device_list">
				<view v-for="(item,index) in list" :key="index">
					<app-deviceCard :itemDetail="item" :index="index" :active="active"
						@setActive="setActive"></app-deviceCard>
				</view>
				<u-divider text="加载中" v-if="loading"></u-divider>
				<view v-else>
					<view class="list" v-if="list.length==0 && finish">
						<u-empty mode="data" text="搜索不到任何结果哦"></u-empty>
					</view>
					<u-divider text="暂无更多数据" v-if="list.length>0 && finish"></u-divider>
				</view>
				
				
			</view>
		</scroll-view>
	</view>
</template>

<script>
	const systemInfo = uni.getSystemInfoSync();
	const safeArea = systemInfo.safeArea;
	export default {
		data() {
			return {
				active: -1,
				loading: false,
				triggered: false,
				loading: false,
				triggered: false,
				finish: false,
				total: 0,
				list: [],
				query: {
					page: 1,
					pageSize: 20,
					name:""
				},
				typeName:"设备列表"
			}
		},
		onLoad(options) {
			if (options.name) {
				this.$set(this.query, 'name', options.name)
			}
			if (options.type_id) {
				this.$set(this.query, 'type_id', options.type_id)
			}
			if (options.status) {
				this.$set(this.query, 'device_status', options.status)
			}
			
			if (options.type_name) {
				this.typeName = options.type_name
				uni.setNavigationBarTitle({
					title: this.typeName
				})
			}

			this.getList()
		},
		computed: {
			scrollHeight() {
				return 'calc(100vh - 100rpx)'
			},
		},
		methods: {
			search(){
				this.list = []
				this.query.page = 1
				this.getList()
			},
			setActive(res) {
				this.active = res
			},
			handleRefresh(e) {
				this.triggered = true;
				setTimeout(() => {
					this.query.page = 1
					this.list = []
					this.getList()
				}, 500)
			},
			lower() {
				if (this.list.length < this.total) {
					this.query.page = this.query.page + 1
					this.getList()
				} else {
					this.finish = true
				}
			},
			getList() {
				this.loading = true
				this.$http.post('/data/api.auth.User/devicelists',{...this.query}).then(res => {
					this.list = [...this.list, ...res.data.list]
					this.total = res.data.page.total || 0
					this.loading = false
					this.triggered = false;
					this.finish = true
				}).catch(() => {
					this.list = []
					this.loading = false
				})
			},

		}
	}
</script>

<style scoped lang="scss">
	.device_list {




		.list {
			border-radius: 20rpx;
			margin: 20rpx;
			padding: 20rpx;
			background: rgba(255, 255, 255, 1);


		}
	}

	.search {

		height: 100rpx;
		background-color: #FFFFFF;

		.input {
			margin: 0 20rpx;
			padding: 0 10rpx;
			height: 80rpx;
			flex: 1;
			background: #F3F2F2;
			border-radius: 46px;

			input {
				// border: none;
				flex: 1;
				font-size: 14px;
				margin: 0 10rpx;
			}

			.btn {
				width: 156rpx;
				height: 68rpx;
				background: rgba(79, 111, 246, 1);
				border-radius: 34rpx;
				color: #FFFFFF;
				font-size: 30rpx;
			}

		}
	}
</style>